package gandalf.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Cart implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToOne
    private Stranka stranka;
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.EAGER)
    private List<Postavka> postavka;
    
    public Cart() {
        postavka = new ArrayList<Postavka>();
    }


    public int getKolicina(Long idVstopnica) {
        for(Postavka p : postavka) {
            if(p.getVstopnica().getId().equals(idVstopnica)) {
                return p.getKolicina();
            }
        }

        return 1;
    }

    public boolean hasVstopnica(Long idVstopnica) {

        for(Postavka p : postavka) {
            if(p.getVstopnica().getId().equals(idVstopnica)) {
                return true;
            }
        }

        return false;

    }

    /**
     * Get the value of vsotaCene
     *
     * @return the value of vsotaCene
     */
    public BigDecimal getVsotaCene() {
        BigDecimal vsota = new BigDecimal("0");
        for(Postavka p : getPostavka()) {
            vsota = vsota.add(p.getVsota());
        }

        return vsota;
    }

    /**
     * Get the value of postavka
     *
     * @return the value of postavka
     */
    public List<Postavka> getPostavka() {
        return postavka;
    }

    /**
     * Set the value of postavka
     *
     * @param postavka new value of postavka
     */
    public void setPostavka(List<Postavka> postavka) {
        this.postavka = postavka;
    }

    /**
     * Get the value of stranka
     *
     * @return the value of stranka
     */
    public Stranka getStranka() {
        return stranka;
    }

    /**
     * Set the value of stranka
     *
     * @param stranka new value of stranka
     */
    public void setStranka(Stranka stranka) {
        this.stranka = stranka;
    }

    /**
     * Get the value of id
     *
     * @return the value of id
     */
    public Long getId() {
        return id;
    }

    /**
     * Set the value of id
     *
     * @param id new value of id
     */
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "gandalf.model.Cart[id=" + id + "]";
    }

    public void updatePostavke(List<Postavka> postavke) {
        for(Postavka p : postavke) {
            if(hasVstopnica(p.getVstopnica().getId())) {
                postavka.get(indexOfPostavkaWithVstopnica(p.getVstopnica().getId())).setKolicina(p.getKolicina());
            } else {
                postavka.add(p);
            }
        }
    }
    public void odstraniPostavko(Long idPostavke) {
        
        Postavka po = null;
        for(Postavka p : postavka) {
          System.out.println("pogoj 1");
          if(p.getId().equals(idPostavke)){
              System.out.println("pogoj 2");
              System.out.println("pred" + postavka.toString());
          //postavka.remove(p);
          po = p;

          
          }
           
           
        }
        if(po!=null){
             System.out.println("pogoj3");
            postavka.remove(po);
            System.out.println("po "  + postavka.toString());
        }
    }

    private int indexOfPostavkaWithVstopnica(Long idVstopnica) {
        int i = 0;
        for(Postavka p : postavka) {
            if(p.getVstopnica().getId().equals(idVstopnica)) {
                return i;
            }
            i++;
        }

        return -1;
    }
}
